
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.sql.*;
import java.util.Calendar;

import com.hogofwar.DJbot.BasePlugin;
import com.hogofwar.DJbot.IRCBase;

public class COM_seen implements BasePlugin {

	public void start(final IRCBase irc, final String[] extra, String channel,
			String sender, String hostname, boolean usename) {
		// System.out.println(extra[0]);
		// System.out.println(extra[1]);

		String start = "";
		if (usename == true) {
			start = sender + ": ";
		}

		if (extra.length != 0) {
			if (extra[0] != " ") {
				String tosend = "I've never seen " + extra[0];
				try {
					Class.forName("org.sqlite.JDBC");
					Connection conn = DriverManager
							.getConnection("jdbc:sqlite:chat.db");
					Statement stat = conn.createStatement();
					ResultSet rs = stat
							.executeQuery("select * from people where name='"
									+ extra[0].toLowerCase()
									+ "' ORDER BY time DESC LIMIT 1;");
					Calendar cal = Calendar.getInstance();
					String message = "";
					while (rs.next()) {
						long timeInMillis = rs.getLong("time");

						cal.setTimeInMillis(timeInMillis);
						message = rs.getString("message");
					}
					if(!message.equalsIgnoreCase("") && message !=null){
					String hour = ""+cal.get(Calendar.HOUR);
				if(cal.get(Calendar.HOUR)<10){
						hour = String.format("%02d", cal.get(Calendar.HOUR));
					}	
				String minute= ""+cal.get(Calendar.MINUTE);
				if(cal.get(Calendar.MINUTE)<10){
						minute = String.format("%02d", cal.get(Calendar.MINUTE));
					}	
					tosend = extra[0] + " was last seen at "
							+ hour + ":"
							+ minute + " on "
							+ cal.get(Calendar.DAY_OF_MONTH) + "/"
							+ (cal.get(Calendar.MONTH) + 1) + "/"
							+ cal.get(Calendar.YEAR) + " saying: " + message;
					// System.out.println("name = " + rs.getString("name"));
					// System.out.println("job = " +
					// rs.getString("message"));
					}
					rs.close();
					conn.close();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// address.getCanonicalHostName()
				// tosend = " seems to be up.";

				irc.sendMessage(channel, start + "" + tosend);
			} else {

				irc.sendMessage(channel, start + "You need to specify a user!");
			}
		} else {

			irc.sendMessage(channel, start + "You need to specify a user!");
		}
	}

	public String help() {
		return "Seen - seen [username] - Tells you the last thing the specified user said.";
		// TODO Auto-generated method stub

	}

	// public static boolean exists(String URLName){
	// try {
	// HttpURLConnection.setFollowRedirects(true);
	// // note : you may also need
	// //HttpURLConnection.setInstanceFollowRedirects(false)
	// HttpURLConnection con =
	// (HttpURLConnection) new URL(URLName).openConnection();
	// con.setRequestMethod("HEAD");
	// return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
	// }
	// catch (Exception e) {
	// e.printStackTrace();
	// return false;
	// }
	// }
	public String version() {
		return "Haspaid version 1";
		// TODO Auto-generated method stub

	}

	@Override
	public String restrict() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean hide() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String aliases() {
		// TODO Auto-generated method stub
		return "lastseen, whenseen, hasseen, checkseen";
	}

}
